package ph.com.gs3.formalistics.custom.implementation;

import ph.com.gs3.formalistics.global.interfaces.CommonCallbackCommand;
import ph.com.gs3.formalistics.global.utilities.logging.FLLogger;
import ph.com.gs3.formalistics.model.RemoteDataAccessObject.RDAOCommunicationException;
import ph.com.gs3.formalistics.model.rdao.PrintRDAO;
import ph.com.gs3.formalistics.presenter.CustomImplementation;
import android.os.AsyncTask;

public class SmartmaticBackend {

	public static final String TAG = SmartmaticBackend.class.getSimpleName();

	public void getPrintableHTMLOfDocument(String documentId, CommonCallbackCommand callbackCommand) {

		GetPrintableHTMLDocumentTask task = new GetPrintableHTMLDocumentTask(callbackCommand);
		task.execute(documentId);

	}

	public static class GetPrintableHTMLReceivingCorrectionReportTask extends
	        AsyncTask<Void, Void, String> {

		private CommonCallbackCommand callbackCommand;

		public GetPrintableHTMLReceivingCorrectionReportTask(CommonCallbackCommand callbackCommand) {
			this.callbackCommand = callbackCommand;
		}

		@Override
		protected String doInBackground(Void... params) {

			PrintRDAO printRDAO = new PrintRDAO(CustomImplementation.SERVER_SMARTMATIC);

			try {
				String printableHtmlViewString = printRDAO
				        .getPrintableReceivingCorrectionReportHTMLView();

				FLLogger.d(TAG, printableHtmlViewString);

				return printableHtmlViewString;

			} catch (RDAOCommunicationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);

			callbackCommand.execute(result);
		}

	}

	private static class GetPrintableHTMLDocumentTask extends AsyncTask<String, Void, String> {

		private CommonCallbackCommand callbackCommand;

		public GetPrintableHTMLDocumentTask(CommonCallbackCommand callbackCommand) {
			this.callbackCommand = callbackCommand;
		}

		@Override
		protected String doInBackground(String... params) {

			PrintRDAO printRDAO = new PrintRDAO(CustomImplementation.SERVER_SMARTMATIC);

			try {
				String printableHtmlViewString = printRDAO.getPrintableHTMLView(params[0]);

				return printableHtmlViewString;

			} catch (RDAOCommunicationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);

			callbackCommand.execute(result);
		}

	}

}
